<template>
    <div class="post-lists">
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['pages/savePage']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="序号" type="index"  min-width="100" :index="indexAdd" />
                <el-table-column label="名称" prop="name" min-width="100" />
                <!-- <el-table-column label="图片地址" prop="pics" min-width="100" /> -->
                <el-table-column label="页面地址" prop="link_type" min-width="240" >
                    <template #default="{ row }">
						<el-link type="primary" :underline="false" v-copy="`${baseUrl}h5/#/diagram?page_id=${row.id}`">{{`${baseUrl}h5/#/diagram?page_id=${row.id}`}}</el-link>
					</template>
                </el-table-column>
                <!-- <el-table-column label="跳转地址" prop="link" min-width="220" /> -->
                <el-table-column label="添加时间" prop="create_time" min-width="180" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['pages/savePage']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['pages/delete']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>
<script lang="ts" setup name="post">
import { imgDelete, imgLists } from '@/api/operation'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import router, { getRoutePath } from '@/router'

const baseUrl = `${import.meta.env.VITE_URL_LINK || ''}`

const showEdit = ref(false)
const queryParams = reactive({
    
})

const indexAdd = (index: number) => {
    const page = pager.page // 当前页码
    const pagesize = pager.size // 每页条数
    return index + 1 + (page - 1) * pagesize
}
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: imgLists,
    params: queryParams
})

const handleAdd = async () => {
    router.push({ path: getRoutePath('pages/savePage') })
}

const handleEdit = async (row: any) => {
    router.push({ path: getRoutePath('pages/savePage'), query: { id: row.id } })
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await imgDelete({ id })
    getLists()
}

getLists()
</script>
